package own.stu.jobgib.playown.redis.redisson;

import org.redisson.Redisson;
import org.redisson.api.*;
import org.redisson.client.codec.Codec;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;

import java.util.concurrent.TimeUnit;

public class CacheTest {

    public static void main(String[] args) {
        // 1. Create config object
        Config config = new Config();
        config.useSingleServer()
                .setTimeout(10 * 60 * 1000)
                .setConnectTimeout(10 * 60 * 1000)
                .setAddress("redis://rediss.server:6379"); // 修改hosts文件

        //使用json序列化方式
        Codec codec = new JsonJacksonCodec();
        config.setCodec(codec);

        // Sync and Async API
        RedissonClient redisson = Redisson.create(config);
        /*final RMap<Object, Object> rMap = redisson.getMap("map1");
        rMap.put("a", "b");
        System.out.println(rMap.getOrDefault("a", "null"));*/

        // ----- Rmap的子类 RMapCache ---
        // 指定subKey 的过期时间，实现是结合zset来设置具体的subKey的过期时间
        final RMapCache<Object, Object> cache = redisson.getMapCache("c-map");
        cache.put("a", "a-v", 10, TimeUnit.MINUTES);
        cache.put("b", "b-v", 5, TimeUnit.MINUTES);
        System.out.println(cache.get("a"));

        // ----- Rmap的子类 RLocalCachedMap ---
        /*final RLocalCachedMap localCachedMap = redisson.getLocalCachedMap("l-map", LocalCachedMapOptions.defaults());
        localCachedMap.put("a", "a - v");
        localCachedMap.put("b", "b - v");
        System.out.println(localCachedMap.get("a"));*/
    }
}
